<template>
  <div class="comment-management">
   <!-- <el-card class="filter-card">
  <el-form :inline="true">
    <el-form-item label="状态过滤">
      <el-select 
        v-model="filterStatus" 
        placeholder="全部状态"
        clearable
        @change="fetchComments"
        style="width: 120px"
      >
        <el-option label="全部" value="" />
        <el-option label="已禁用" :value="0" />
        <el-option label="已通过" :value="1" />
        <el-option label="已拒绝" :value="2" />
      </el-select>
    </el-form-item>
  </el-form>
</el-card> -->

    <el-card class="table-card">
      <el-table 
        :data="comments"
        v-loading="loading"
        style="width: 100%"
        stripe
      >
        <el-table-column prop="commentId" label="ID" width="70" align="center" />
        
        <el-table-column label="评论内容" min-width="220">
          <template #default="{ row }">
            <div class="comment-content">
              {{ row.comment }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="用户信息" width="180">
          <template #default="{ row }">
            <div class="user-info">
              <el-avatar :src="'http://localhost:8092' + row.avatar" />
              <div class="user-meta">
                <div class="nickname">{{ row.nickname }}</div>
                <div class="user-id">用户ID: {{ row.userId }}</div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="所属话题" width="120" align="center">
          <template #default="{ row }">
            <el-tag type="info">#{{ row.topicId }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="评论时间" width="160">
          <template #default="{ row }">
            <div class="time">
              {{ formatDate(row.createdAt) }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="审核状态" width="110" align="center">
          <template #default="{ row }">
            <el-tag 
              :type="statusStyle[row.approvalStatus].type"
              effect="light"
            >
              {{ statusStyle[row.approvalStatus].text }}
            </el-tag>
          </template>
        </el-table-column>

     <el-table-column label="操作" width="150" fixed="right" align="center">
  <template #default="{ row }">
    <div class="actions">
      <!-- 当状态为已通过时显示禁用按钮 -->
      <el-button 
        v-if="row.approvalStatus === 1"
        type="warning" 
        size="small"
        @click.stop="handleApprove(row.commentId, 0)"
      >
        禁用
      </el-button>
      
      <!-- 当状态为已禁用时显示恢复按钮 -->
      <el-button 
        v-if="row.approvalStatus === 0"
        type="success" 
        size="small"
        @click.stop="handleApprove(row.commentId, 1)"
      >
        恢复
      </el-button>
      
      <!-- 保留拒绝状态操作 -->
      <el-button 
        v-if="row.approvalStatus === 2"
        type="info"
        size="small"
        disabled
      >
        已拒绝
      </el-button>
    </div>
  </template>
</el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const loading = ref(false)
const comments = ref([])
const filterStatus = ref('')

const statusStyle = {
  0: { text: '已禁用', type: 'danger' }, // 修改0状态显示
  1: { text: '已通过', type: 'success' },
  2: { text: '已拒绝', type: 'info' }
};

const formatDate = (dateStr) => {
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  }).replace(/\//g, '-')
}

const fetchComments = async () => {
  try {
    loading.value = true
    const params = { status: filterStatus.value || undefined }
    const { data } = await axios.get('http://localhost:8092/comment/admin/list', { 
      params,
      headers: {
        Authorization: `Bearer ${localStorage.getItem('token')}`
      }
    })
    
    if (data.code === 200) {
      comments.value = data.data
    }
  } catch (error) {
    ElMessage.error('获取评论失败: ' + (error.response?.data?.msg || error.message))
  } finally {
    loading.value = false
  }
}

const handleApprove = async (commentId, status) => {
  try {
    await axios.post('http://localhost:8092/comment/admin/approve-comment', 
      null,
      {
        params: {
          commentId: commentId,
          approvalStatus: status
        },
        headers: {
          Authorization: `Bearer ${localStorage.getItem('token')}`
        }
      }
    )
    ElMessage.success('操作成功')
    await fetchComments()
  } catch (error) {
    ElMessage.error('操作失败: ' + (error.response?.data?.msg || error.message))
  }
}

onMounted(() => {
  fetchComments()
})
</script>

<style scoped>
.comment-management {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.filter-card {
  margin-bottom: 15px;
  :deep(.el-card__body) {
    padding: 15px 20px;
  }
}

.table-card {
  margin-top: 15px;
  :deep(.el-card__body) {
    padding: 5px;
  }
}

.user-info {
  display: flex;
  align-items: center;
  padding: 5px 0;
  .el-avatar {
    width: 36px;
    height: 36px;
    margin-right: 10px;
  }
  .user-meta {
    line-height: 1.4;
    .nickname {
      font-size: 13px;
      color: #333;
    }
    .user-id {
      font-size: 12px;
      color: #999;
    }
  }
}

.comment-content {
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  word-break: break-word;
}

.time {
  font-size: 13px;
  color: #666;
}

.actions {
  display: flex;
  gap: 5px;
  .el-button {
    padding: 6px 10px;
  }
}

.no-action {
  color: #999;
  font-size: 12px;
}

.el-table {
  :deep(th.el-table__cell) {
    background-color: #f8f9fa;
  }
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
}
</style>